/*
 * JVM_Heap.h
 *
 *  Created on: 06.01.2011
 *      Author: Tavin
 */

#ifndef JVM_HEAP_H_
#define JVM_HEAP_H_

#include <stdint.h>


#include "board.h"
#include "JVM_StatusCodes.h"
#include "JVM_DataTypes.h"
uint8_t heap[HEAP_SIZE];

#endif /* JVM_HEAP_H_ */



/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_clearBlock							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.11			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_clearBlock(uint16_t heapAdress,uint16_t size)
{
uint16_t counter;

	if ((heapAdress<0) || (heapAdress+size>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);

	for (counter=heapAdress;counter<heapAdress+size;counter++)
		{
		heap[counter]=0;
		}

	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setByte								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setByte(uint16_t heapAdress,uint8_t value)
{
if ((heapAdress<0) || (heapAdress>HEAP_SIZE))
		return (STCD_HEAP_OVERFLOW);

	heap[heapAdress]=value;
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setShort							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setShort(uint16_t heapAdress,uint16_t value)
{
if ((heapAdress<0) || ((heapAdress+1)>HEAP_SIZE))
		return (STCD_HEAP_OVERFLOW);

	heap[heapAdress++]=value>>8;
	heap[heapAdress]=value&0xff;
	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setInt								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setInt(uint16_t heapAdress,uint32_t value)
{
if ((heapAdress<0) || ((heapAdress+3)>HEAP_SIZE))
		return (STCD_HEAP_OVERFLOW);

	heap[heapAdress++]=(value>>24)&0xff;;
	heap[heapAdress++]=(value>>16)&0xff;;
	heap[heapAdress++]=(value>>8)&0xff;;
	heap[heapAdress]=value&0xff;
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setFloat							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setFloat(uint16_t heapAdress,float value)
{
	union data_value_32 myValue;

	myValue.floatValue=value;

	return(JVM_Heap_setInt(heapAdress,myValue.floatValue));
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setLong								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setLong(uint16_t heapAdress,uint64_t value)
{
if ((heapAdress<0) || ((heapAdress+7)>HEAP_SIZE))
		return (STCD_HEAP_OVERFLOW);

	heap[heapAdress++]=(value>>56)&0xff;
	heap[heapAdress++]=(value>>48)&0xff;
	heap[heapAdress++]=(value>>40)&0xff;
	heap[heapAdress++]=(value>>32)&0xff;

	heap[heapAdress++]=(value>>24)&0xff;
	heap[heapAdress++]=(value>>16)&0xff;
	heap[heapAdress++]=(value>>8)&0xff;;
	heap[heapAdress]=value&0xff;
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_setDouble							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_setDouble(uint16_t heapAdress,double value)
{
	union data_value_64 myValue;

	myValue.doubleValue=value;

	return(JVM_Heap_setLong(heapAdress,myValue.doubleValue));
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getShort								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getRealAddres(uint16_t heapAdress,uint32_t * addres)
{
	if ((heapAdress<0) || ((heapAdress+1)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);

	//*addres=(uint32_t*)&heap[heapAdress];
	*addres=(uint32_t)&heap[heapAdress];
	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getByte								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getByte(uint16_t heapAdress,int8_t * value)
{
	if ((heapAdress<0) || (heapAdress>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);
	*value=heap[heapAdress];

	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getShort								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getShort(uint16_t heapAdress,int16_t * value)
{
	if ((heapAdress<0) || ((heapAdress+1)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);
	*value=heap[heapAdress++]<<8;
	*value|=heap[heapAdress];
	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getChar								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getChar(uint16_t heapAdress,uint16_t * value)
{
	if ((heapAdress<0) || ((heapAdress+1)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);
	*value=heap[heapAdress++]<<8;
	*value|=heap[heapAdress];
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getInt								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getInt(uint16_t heapAdress,int32_t * value)
{
	if ((heapAdress<0) || ((heapAdress+3)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);

	*value=heap[heapAdress++]<<24;
	*value|=heap[heapAdress++]<<16;
	*value|=heap[heapAdress++]<<8;
	*value|=heap[heapAdress];
	return(STCD_OK);
}



/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getReference						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.15			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getReference(uint16_t heapAdress,uint32_t * value)
{
	if ((heapAdress<0) || ((heapAdress+3)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);

	*value=heap[heapAdress++]<<24;
	*value|=heap[heapAdress++]<<16;
	*value|=heap[heapAdress++]<<8;
	*value|=heap[heapAdress];
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getFloat							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.11			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getFloat(uint16_t heapAdress,float * value)
{
	union data_value_32 myValue;
	uint16_t returnValue;

	returnValue=JVM_Heap_getInt(heapAdress,&myValue.intValue);
	*value=myValue.floatValue;
	return(returnValue);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getLong								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.6			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getLong(uint16_t heapAdress,int64_t * value)
{
	if ((heapAdress<0) || ((heapAdress+7)>HEAP_SIZE))
			return (STCD_HEAP_OVERFLOW);


	*value=(uint64_t)heap[heapAdress++]<<56;
	*value|=(uint64_t)heap[heapAdress++]<<48;
	*value|=(uint64_t)heap[heapAdress++]<<40;
	*value|=(uint64_t)heap[heapAdress++]<<32;
	*value|=heap[heapAdress++]<<24;
	*value|=heap[heapAdress++]<<16;
	*value|=heap[heapAdress++]<<8;
	*value|=heap[heapAdress];
	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Heap_getDouble							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.11			Status : temp   			*/
/*																						*/
/****************************************************************************************/

uint16_t static inline JVM_Heap_getDouble(uint16_t heapAdress,double * value)
{
	union data_value_64 myValue;

	uint16_t returnValue;

	returnValue=JVM_Heap_getLong(heapAdress,&myValue.longValue);
	*value=myValue.doubleValue;

	return(returnValue);
}

